﻿using Models.Common;
using Models.Device;
using Models.JsonResultModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using UtilsBase;
using UtilsCache;
using ServiceWeb.DBGateway;

namespace ServiceWeb.APIRequest
{
    public class ServiceDeviceControl
    {

        public bool sendDeviceCommand(string dguid, string key, string from, string commandCentent = "")
        {
            ModelRequestDeviceControl deviceControl = new ModelRequestDeviceControl();
            deviceControl.guid = Guid.NewGuid().ToString();
            deviceControl.dguid = dguid;
            deviceControl.key = key;
            deviceControl.from = from;
            deviceControl.commandName = EnumDeviceControlCommandName.deviceCommand;
            deviceControl.content = commandCentent;
           
            //提交办理
            //new UtilsHttpHelper().sendPostToGateWayCloodBridgeAPI(UtilAppsettingCache.getSetting("API:url_deviceControl"), deviceControl);

          //  Task.Run(() =>
           // {
                new ServiceWebRabbitMQSendMsg_UseMethod().sendDeviceControlCommand(deviceControl);
                //写入待办表
                //new DBIotDeviceControlApiRequestLog().add(deviceControl.guid, JsonConvert.SerializeObject(deviceControl), new MagerLoginInfo());
           // });

            return true;
        }

        public bool sendbashCommand(string gwcode, string content, string from)
        {
            ModelRequestDeviceControl deviceControl = new ModelRequestDeviceControl();
            deviceControl.guid = Guid.NewGuid().ToString();
            deviceControl.gwcode = gwcode;
            deviceControl.content = content;
            deviceControl.from = from;
            deviceControl.commandName = EnumDeviceControlCommandName.bashCommand;

            new ServiceWebRabbitMQSendMsg_UseMethod().sendDeviceControlCommand(deviceControl);
            new DBIotDeviceControlApiRequestLog().add(deviceControl.guid, JsonConvert.SerializeObject(deviceControl), new MagerLoginInfo());
           
            //new UtilsHttpHelper().sendPostToGateWayCloodBridgeAPI(UtilAppsettingCache.getSetting("API:url_deviceControl"), deviceControl);
            return true;
        }
        public bool upGradeMQTTConfig(string gwcode, MQTTInfo mQTTInfo, string from)
        {
            ModelRequestDeviceControl deviceControl = new ModelRequestDeviceControl();
            deviceControl.guid = Guid.NewGuid().ToString();
            deviceControl.gwcode = gwcode;
            deviceControl.content = JsonConvert.SerializeObject(mQTTInfo);// new StringContent(JsonConvert.SerializeObject(mQTTInfo), Encoding.UTF8, "application/json"); //
            deviceControl.from = from;
            deviceControl.commandName = EnumDeviceControlCommandName.upGradeMQTTConfig;

            new ServiceWebRabbitMQSendMsg_UseMethod().sendDeviceControlCommand(deviceControl);
            new DBIotDeviceControlApiRequestLog().add(deviceControl.guid, JsonConvert.SerializeObject(deviceControl), new MagerLoginInfo());

            //new UtilsHttpHelper().sendPostToGateWayCloodBridgeAPI(UtilAppsettingCache.getSetting("API:url_deviceControl"), deviceControl);
            return true;
        }

        public bool upConfigAppSetting(string gwcode, ModelConfigKeyValue model, string from)
        {
            ModelRequestDeviceControl deviceControl = new ModelRequestDeviceControl();
            deviceControl.guid = Guid.NewGuid().ToString();
            deviceControl.gwcode = gwcode;
            deviceControl.content = JsonConvert.SerializeObject(model);
            deviceControl.from = from;
            deviceControl.commandName = EnumDeviceControlCommandName.upConfigAppSetting;
            new ServiceWebRabbitMQSendMsg_UseMethod().sendDeviceControlCommand(deviceControl);
            new DBIotDeviceControlApiRequestLog().add(deviceControl.guid, JsonConvert.SerializeObject(deviceControl), new MagerLoginInfo());

            //new UtilsHttpHelper().sendPostToGateWayCloodBridgeAPI(UtilAppsettingCache.getSetting("API:url_deviceControl"), deviceControl);

            return true;
        }
        public bool updateGradeSoftware(string gwcode, string from)
        {
            ModelRequestDeviceControl deviceControl = new ModelRequestDeviceControl();
            deviceControl.guid = Guid.NewGuid().ToString();
            deviceControl.gwcode = gwcode;
            deviceControl.from = from;
            deviceControl.commandName = EnumDeviceControlCommandName.updateGradeSoftware;
            new ServiceWebRabbitMQSendMsg_UseMethod().sendDeviceControlCommand(deviceControl);
            new DBIotDeviceControlApiRequestLog().add(deviceControl.guid, JsonConvert.SerializeObject(deviceControl), new MagerLoginInfo());

            //new UtilsHttpHelper().sendPostToGateWayCloodBridgeAPI(UtilAppsettingCache.getSetting("API:url_deviceControl"), deviceControl);

            return true;
        }
    }
}
